package com.finance.cooperate.data.service.impl;

import com.alibaba.fastjson.JSON;
import com.finance.cooperate.common.enumerate.ScrapyAppCategoriesEnum;
import com.finance.cooperate.common.utils.DozerMapper;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.dao.entity.primary.App;
import com.finance.cooperate.dao.local.scope.CooperateRequestScope;
import com.finance.cooperate.dao.repository.primary.AppRepository;
import com.finance.cooperate.dao.support.dto.ScrapyAppCategoriesDTO;
import com.finance.cooperate.data.dto.AppDTO;
import com.finance.cooperate.data.service.AppService;
import com.finance.cooperate.data.service.ScrapyAppsCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName AppServiceImpl
 * @Description TODO
 * @Author shen
 * @Date 2023/8/10 21:32
 * @Modify ...
 */
@Slf4j
@Service
public class AppServiceImpl implements AppService {

    @Autowired
    private AppRepository appRepository;

    @Autowired
    private ScrapyAppsCacheService scrapyAppsCacheService;

    @Autowired
    private CooperateRequestScope requestScope;



    @Override
    public String getAppList(String uid) {

        // 跑特征取新数
        Date lastSubmitTime = requestScope.getLastSubmitTime();

        List<App> appList = appRepository.getCurrentApp(uid, requestScope.getDeviceId(), lastSubmitTime);

        List<AppDTO> appDTOS = DozerMapper.mapList(appList, AppDTO.class);

        return JSON.toJSONString(appDTOS);

    }

    @Override
    public String getScrapyAppList(String uid) {
        // 爬虫app 字典定义{app_type, [app_install_time_01, app_install_time_02, ...]}
        Map<String, List<String>> appDict = new HashMap<>();

        // 跑特征取新数
        Date lastSubmitTime = requestScope.getLastSubmitTime();

        List<App> appList = appRepository.getCurrentApp(uid, requestScope.getDeviceId(), lastSubmitTime);


        for (App app: appList) {

            // 所有类目
            List<String> listType = appDict.getOrDefault("all_type", new ArrayList<String>());
            listType.add(app.getFirstInstallTime());
            appDict.put("all_type", listType);


            String packageName = app.getAppPackageName();

            if (!StringUtil.isEmptyOrNvl(packageName)) {

                // 爬虫类别遍历
                ScrapyAppCategoriesEnum[] enums = ScrapyAppCategoriesEnum.values();

                for (ScrapyAppCategoriesEnum e : enums) {

                    if (e == ScrapyAppCategoriesEnum.Top_Overall || e == ScrapyAppCategoriesEnum.Top_Apps || e == ScrapyAppCategoriesEnum.Top_New) {
                        continue;
                    }

                    List<ScrapyAppCategoriesDTO> scrapyAppLists = scrapyAppsCacheService.getByCategories(e.getCode());

                    for (ScrapyAppCategoriesDTO scrapy: scrapyAppLists) {

                        if (packageName.toLowerCase().equals(scrapy.getStoresId().toLowerCase())) {// 匹配上了

                            Optional<ScrapyAppCategoriesEnum> optional = Arrays.stream(ScrapyAppCategoriesEnum.values())
                                    .filter(o -> o.getCode().equals(scrapy.getCategoriesId()))
                                    .findFirst();

                            if (optional.isPresent()) {

                                ScrapyAppCategoriesEnum anEnum = optional.get();

                                if (anEnum == ScrapyAppCategoriesEnum.Android_Wear) {

                                    List<String> list = appDict.getOrDefault("android_wear", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("android_wear", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Art_Design) {

                                    List<String> list = appDict.getOrDefault("art_design", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("art_design", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Auto_Vehicles) {

                                    List<String> list = appDict.getOrDefault("auto_vehicles", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("auto_vehicles", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Beauty) {

                                    List<String> list = appDict.getOrDefault("beauty", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("beauty", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Books_Reference) {

                                    List<String> list = appDict.getOrDefault("books_reference", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("books_reference", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Business) {

                                    List<String> list = appDict.getOrDefault("business", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("business", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Communication) {

                                    List<String> list = appDict.getOrDefault("communication", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("communication", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Comics) {

                                    List<String> list = appDict.getOrDefault("comics", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("comics", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Dating) {

                                    List<String> list = appDict.getOrDefault("dating", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("dating", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Education) {

                                    List<String> list = appDict.getOrDefault("education", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("education", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Entertainment) {

                                    List<String> list = appDict.getOrDefault("entertainment", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("entertainment", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Events) {

                                    List<String> list = appDict.getOrDefault("events", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("events", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Family) {

                                    List<String> list = appDict.getOrDefault("family", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("family", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Finance) {

                                    List<String> list = appDict.getOrDefault("finance", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("finance", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Food_Drink) {

                                    List<String> list = appDict.getOrDefault("food_drink", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("food_drink", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Games) {

                                    List<String> list = appDict.getOrDefault("games", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("games", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Health_Fitness) {

                                    List<String> list = appDict.getOrDefault("health_fitness", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("health_fitness", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.House_Home) {

                                    List<String> list = appDict.getOrDefault("house_home", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("house_home", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Libraries_Demo) {

                                    List<String> list = appDict.getOrDefault("libraries_demo", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("libraries_demo", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Lifestyle) {

                                    List<String> list = appDict.getOrDefault("lifestyle", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("lifestyle", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Maps_Navigation) {

                                    List<String> list = appDict.getOrDefault("maps_navigation", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("maps_navigation", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Medical) {

                                    List<String> list = appDict.getOrDefault("medical", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("medical", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Music_Audio) {

                                    List<String> list = appDict.getOrDefault("music_audio", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("music_audio", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.News_Magazines) {

                                    List<String> list = appDict.getOrDefault("news_magazines", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("news_magazines", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Parenting) {

                                    List<String> list = appDict.getOrDefault("parenting", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("parenting", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Personalization) {

                                    List<String> list = appDict.getOrDefault("personalization", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("personalization", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Photography) {

                                    List<String> list = appDict.getOrDefault("photography", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("photography", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Productivity) {

                                    List<String> list = appDict.getOrDefault("productivity", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("productivity", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Shopping) {

                                    List<String> list = appDict.getOrDefault("shopping", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("shopping", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Social) {

                                    List<String> list = appDict.getOrDefault("social", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("social", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Sports) {

                                    List<String> list = appDict.getOrDefault("sports", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("sports", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Tools) {

                                    List<String> list = appDict.getOrDefault("tools", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("tools", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Travel_Local) {

                                    List<String> list = appDict.getOrDefault("travel_local", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("travel_local", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Video_Players_Editors) {

                                    List<String> list = appDict.getOrDefault("video_players_editors", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("video_players_editors", list);

                                } else if (anEnum == ScrapyAppCategoriesEnum.Weather) {

                                    List<String> list = appDict.getOrDefault("weather", new ArrayList<String>());
                                    list.add(app.getFirstInstallTime());
                                    appDict.put("weather", list);

                                }


                            }

                            break;
                        }
                    }
                }
            }
        }


        return JSON.toJSONString(appDict);
    }


}
